<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/4
 * Time: 16:09
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\AreaModel;
use App\Models\LogModel;
use App\Models\Main\CompanyContractModel;
use App\Models\OrderCommentModel;
use App\Models\User\UserLevelModel;
use App\Services\OrdersPayService;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;

class OrderBales extends ApiBase
{


    /**
     *新建发布货单或者读取一个未发布的货单
     */
    public function getNewOrder(Request $request){
        /**
         * 先检查是否存在未发布的订单
        */
        $uid=OA::$user->getId();
        $user=OA::$user->getModel();
        if(!$user['is_company']){
            OA::Json(1,'无法发布货单,请到设置打开"启用公司"开关');
        }
        $id=$request->post('id');

        if($id!=''){
            $one=OrderBaleModel::where([
                'id'=>$id,
                'uid'=>$uid,
            ])->first();

        }else{
            $one=OrderBaleModel::where([
                'uid'=>$uid,
                'status'=>OrderBaleModel::status_weikaishi
            ])->first();
         //  var_dump($one->id);   exit("aaaaaaa");
        }

        if(!$one){
            $one=new OrderBaleModel();
            $one->uid=$uid;
            $one->name='';
            $one->styleNo='';
            $one->styleName='';
            $one->outTypeStr='';
            $one->juli=10;

             $one->scence='';
             $one->material='';
             $one->orderType='';
             $one->design='';
             $one->tech='';

            /*  $one->cate=0;
             $one->num=0;
             $one->money=0;
             $one->allmoney=0;
             $one->scence=0;
             $one->material=0;
             $one->tech=0;


             $one->sizelist='';
             $one->img='';
             $one->imgDetails='';
             $one->proType=$uid;
             $one->region=$uid;
             $one->proWay=$uid;

             $one->period=$uid;
             $one->stuff=$uid;
             $one->deplete=$uid;
             $one->orderType=$uid;
             $one->style=$uid;

             $one->detail=$uid;
             $one->is_rob=$uid;
             $one->is_fabric=$uid;
             $one->is_contract=$uid;*/

            $one->status=OrderBaleModel::status_weikaishi;
            $one->del=0;
            $one->created_at=0;
            $one->visible=1;

            $one->updated_at=0;
            $one->bond=0;
            $one->save();
            $one=OrderBaleModel::where([
                'uid'=>$uid,
                'status'=>OrderBaleModel::status_weikaishi
            ])->first();
        }
        $list=$one->toArray();
        $ziliao=OA::$user->getComPanyVal(CompanyModel::type_gongsi, $uid,true);
        if(!$ziliao||!$ziliao['visible']||!$ziliao['shenhe']){
           OA::Json(1,'公司功能未启用');
        }
        $list['comCity']=$ziliao['comCity'];
        $list['period']=Fun::date($list['period'],false);
        $list['complete_time']=Fun::date($list['complete_time'],false);
      //  $list['location']=AreaModel::getMergerName($list['region']);
        OA::Json(0,$list);
    }

    /**
     * 获取货单合同数据
    */
    public function getOrdercontract(Request $request){
        $id=$request->id;
        $Model=CompanyContractModel::where([
            'bale'=>$id,
            'cid'=>OA::$user->getId()
        ])->select('file')->first();
        if($Model){
            OA::Json(0,[
                'small'=>$Model->file
            ]);
        }else{
            OA::Json(0,'');
        }
    }




}